function doBudgetDelete(budgetId){
	var url="/index.php?mod=controller1";
	var parms = new Object();
	parms["budgetId"]= budgetId;
	parms["action"]= "deleteBudget";
	
	$.post(url,parms,function(resultData){
		var errorCode = $(resultData).find("code").text();
		if (errorCode == 0) {
			alert("Budget deleted successfully.");
			top.location = "/index.php?mod=viewBudgets";
		} else {
			alert($(resultData).find("errMessage").text());
		}
	});
}

function promptForCurrentBudget(){
	$("#confirmDlg").html("Do you want to view current budget?<br/>");
	$("#confirmDlg").css('font-size', '0.8em');
	
	$("#confirmDlg").dialog( {
		bgiframe : true,
		autoOpen : false,
		title : "Confirm?",
		modal : true,
		buttons : {
			"No" : function() {
				$(this).dialog('destroy');
			},
			"Yes" : function() {
				showLoading(true);
				top.location ="index.php?mod=editBudget&budgetId=current";
				$(this).dialog('destroy');
			}
		}
	});

	$("#confirmDlg").dialog("open");
}

$(document).ready(function(){
	$(".budgetMonth").click(function(){
		showLoading(true);
	});
	
	promptForCurrentBudget();
	
	$("#msgDlg").dialog( {
		bgiframe :true,
		autoOpen :false,
		modal :true,
		buttons : {
			Ok : function() {
				$(this).dialog('close');
			}
		}
	});
	
	$("#createBudgetLink").click(function(){
		$("#emptyRow").fadeIn("normal");
		$("#newRowTitle").fadeIn("normal");
		$("#newRow").fadeIn("normal");
		$("#submitNewBudgetRow").fadeIn("normal");
		
	});
	
	$("#deleteBudgetLink").click(function(){
		if ($("input[name='selectBudget']:checked").val() == null){
			$("#msgDlg").html("Select the the budget you want to delete.");
			$("#msgDlg").dialog("open");
			return;
		}
		
		$("#confirmDlg").dialog( {
			bgiframe :true,
			autoOpen :false,
			modal :true,
			title: "Delete Budget?",
			buttons : {
				"Cancel": function() {
					$(this).dialog('close');
				},
				"Yes" : function() {
					doBudgetDelete($("input[name='selectBudget']:checked").val());
					$(this).dialog('close');
				}
			}
		});
		
		$("#confirmDlg").css('font-size', '0.8em');
		$("#confirmDlg").html("Are you sure you want to delete this budget?");
		$("#confirmDlg").dialog("open");
		
		
	});
	
	$("#createBudgetButton").click(function(){
		if ($("#budgetMonth").val()+" "+$("#budgetYear").val() == "January 2010"){
			$("#msgDlg").html("You cannot create this budget, try another month and year combination.");
			$("#msgDlg").dialog("open");
			return;
		}
			
		
		if ($("input[name='budgetTemplate']:checked").val() == null){
			$("#msgDlg").html("Select the template you will use for your budget.");
			$("#msgDlg").dialog("open");
			return;
		}
		
		if (($("input[name='budgetTemplate']:checked").val() == "existingBudget") && ($("input[name='selectBudget']:checked").val() == null)) {
			$("#msgDlg").html("Select the budget you wish to copy from.");
			$("#msgDlg").dialog("open");
			return;
		}
		
		var url="/index.php?mod=createBudget";
		var parms = new Object();
		parms["budgetName"]= $("#budgetMonth").val()+" "+$("#budgetYear").val();
		parms["templateType"]= $("input[name='budgetTemplate']:checked").val();
		parms["budgetIdToCopy"]= $("input[name='selectBudget']:checked").val();
		
		$.post(url,parms,function(resultData){
			var errorCode = $(resultData).find("code").text();
			
			//alert($(resultData).find("newBudgetId").text());
			if(errorCode == 0){
				top.location = "/index.php?mod=viewBudgets&budgetId=" + $(resultData).find("newBudgetId").text();
			} else {
				alert($(resultData).find("errMessage").text());
			}
		});
	});
	
	$("#cancelBudgetButton").click(function(){
		$("#emptyRow").fadeOut("normal");
		$("#newRowTitle").fadeOut("normal");
		$("#newRow").fadeOut("normal");
		$("#submitNewBudgetRow").fadeOut("normal");
	});
});